﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Nette
{
    class KVBox : StackPanel
    {

        public KVLine AddEmptyLine()
        {
            var line = new KVLine();
            line.IsSelected = false;
            line.IsCanDelete = false;
            //添加事件
            line.TLostFocus += onInput;
            line.DeleteClick += onDelClick;
            this.Children.Add(line);
            return line;
        }

        public KVBox PutLine(KVLine line)
        {
            //添加事件
            line.TLostFocus += onInput;
            line.DeleteClick += onDelClick;
            this.Children.Insert(0,line);
            return this;
        }


        private KVLine getLastLine()
        {
            return (KVLine)Children[Children.Count - 1];
        }
        private void deleteLine(KVLine kvLine)
        {
            //最后一个不能删除
            Children.Remove(kvLine);
        }


        public KVBox()
        {
            //初始化子元素事件
            foreach (KVLine line in Children) 
            {
                line.TLostFocus += onInput;
                line.DeleteClick += onDelClick;
            }
           
            //默认有一个
            AddEmptyLine();
        }

        private void onInput(object sender, RoutedEventArgs e)
        {
            //查看如果输入内容为不为空字符且为最底部的节点，则添加节点
            KVLine taKvline = (KVLine)sender;
            if ((!taKvline.KeyStr.Equals("") || !taKvline.ValueStr.Equals("")) && taKvline.Equals(getLastLine()))
            {
                taKvline.IsCanDelete = true;
                taKvline.IsSelected = true;
                AddEmptyLine();
            }
        }

        private void onDelClick(object sender, RoutedEventArgs e)
        {
            deleteLine((KVLine)sender);
        }

        /// <summary>
        /// 转换数据为字典，key为string，value为string 或者 string[]
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> getData()
        {
            var dic = new Dictionary<string, string>();
            foreach (KVLine rander in Children)
            {
                if (!rander.KeyStr.Equals("") && !rander.ValueStr.Equals(""))
                {
                    //判断是否已经存在key
                    if (dic.ContainsKey(rander.KeyStr))
                    {
                        dic[rander.KeyStr] = rander.ValueStr;
                    }
                    else
                    {
                        dic.Add(rander.KeyStr, rander.ValueStr);
                    }
                }
            }
            return dic;
        }

        public Dictionary<string, List<string>> getStringArrayDic()
        {
            var dic = new Dictionary<string, List<string>>();
            foreach (KVLine rander in Children)
            {
                if (!rander.KeyStr.Equals("") && !rander.ValueStr.Equals(""))
                {
                    //判断是否已经存在key
                    if (dic.ContainsKey(rander.KeyStr))
                    {
                        dic[rander.KeyStr].Add(rander.ValueStr);
                    }
                    else
                    {
                        List<string> values = new List<string>();
                        values.Add(rander.ValueStr);
                        dic.Add(rander.KeyStr, values);
                    }
                }
            }
            return dic;
        }
    }
}
